﻿package encrypt
{
	import com.hurlant.math.BigInteger;
	import flash.utils.ByteArray;
	import flash.utils.Endian;

	public class RSA
	{
		private var e:BigInteger;
		private var N:BigInteger;
		private var d:BigInteger;
		public function RSA(e:BigInteger , d:BigInteger , N:BigInteger)
		{
			// constructor code
			this.e = e; // public key
			this.N = N; // public key
			this.d = d; // private key


		}
		public function encrypt(message:ByteArray):String{
			var bounder:ByteArray = new ByteArray();
			bounder = message;
			bounder.writeByte(0x1);
			var x:BigInteger = BigIntFactory.Attach(bounder);
			x = x.modPow(e,N);
			return x.toString(16);
		}
		
		public function decrypt(hexInt:String):ByteArray{
			var x:BigInteger = BigIntFactory.Create(hexInt);
			x = x.modPow(d,N);
			var original:ByteArray = BigIntFactory.ToByteArray(x);
			var ret:ByteArray = new ByteArray();
			original.position = 0;
			original.readBytes(ret,0,original.length -1);
			return ret;
			
		}
	}



}